package com.icetec.iceintel.ui.adapter;

import android.content.Context;
import android.media.RingtoneManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;

import com.icetec.R;
import com.icetec.iceintel.database.DevicesListSqlHelper;
import com.icetec.iceintel.model.Alarm;
import com.icetec.iceintel.model.AlarmPreference;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class AlarmPreferenceListAdapter extends BaseAdapter implements Serializable {

    private final String[] repeatDays;
    private  final String[] alarmDifficulties;

    private Context context;
    private Alarm alarm;
    private List<AlarmPreference> preferences = new ArrayList<AlarmPreference>();

    private String[] alarmTones;
    private String[] alarmTonePaths;


    public AlarmPreferenceListAdapter(Context context, Alarm alarm) {

        setContext(context);
        RingtoneManager ringtoneMgr = new RingtoneManager(getContext());
        ringtoneMgr.setType(RingtoneManager.TYPE_ALARM);
        Log.d("APListAdapter", "正在加载铃声");


        alarmTones = new String[2];
        alarmTones[0] = context.getString(R.string.silence);
        alarmTonePaths = new String[2];
        alarmTonePaths[0] = "";
        repeatDays = context.getResources().getStringArray(R.array.repeat_days);
        alarmDifficulties = context.getResources().getStringArray(R.array.alarm_difficulties);

        Log.d("APListAdapter", "Finished Loading " + alarmTones.length + " Ringtones.");
        setAlarm(alarm);
    }


    @Override
    public int getCount() {
        return preferences.size();
    }

    @Override
    public Object getItem(int position) {
        return preferences.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AlarmPreference alarmPreference = (AlarmPreference) getItem(position);
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        switch (alarmPreference.getType()) {
            case BOOLEAN:
                if (null == convertView)
                    convertView = layoutInflater.inflate(android.R.layout.simple_list_item_checked, null);
                CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(android.R.id.text1);
                checkedTextView.setText(alarmPreference.getTitle());
                checkedTextView.setChecked((Boolean) alarmPreference.getValue());
                break;
            case TIME:
            case INTEGER:
            case STRING:
            case LIST:
            case MULTIPLE_LIST:
            case ACTIVITY:
            default:
                if (null == convertView)//|| convertView.getId() != android.R.layout.simple_list_item_2)
                    convertView = layoutInflater.inflate(android.R.layout.simple_list_item_2, null);

                TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
                text1.setTextSize(18);
                text1.setText(alarmPreference.getTitle());

                TextView text2 = (TextView) convertView.findViewById(android.R.id.text2);
                text2.setText(alarmPreference.getSummary());
                break;
        }

        return convertView;
    }

    public Alarm getAlarm() {
        for (AlarmPreference preference : preferences) {
            switch (preference.getKey()) {
                case ALARM_ACTIVE:
                    alarm.setAlarmActive((Boolean) preference.getValue());
                    break;
                case ALARM_NAME:
                    alarm.setAlarmName((String) preference.getValue());
                    break;
                case ALARM_TIME:
                    alarm.setAlarmTime((String) preference.getValue());
                    break;
                case ALARM_TYPE:
                    alarm.setAlarmType(Alarm.AlarmType.valueOf((String) preference.getValue()));
                    break;
                case ALARM_TONE:
                    alarm.setAlarmTonePath((String) preference.getValue());
                    break;
                case ALARM_VIBRATE:
                    alarm.setVibrate((Boolean) preference.getValue());
                    break;
                case ALARM_REPEAT:
                    alarm.setDays((Alarm.Day[]) preference.getValue());
                    break;
                case ALARM_MAC:
                    alarm.setAlarmMAC((String) preference.getValue());
                    break;
            }
        }

        return alarm;
    }

    public void setAlarm(Alarm alarm) {
        this.alarm = alarm;
        preferences.clear();
        preferences.add(new AlarmPreference(AlarmPreference.Key.ALARM_ACTIVE, "激活", null, null, alarm.getAlarmActive(), AlarmPreference.Type.BOOLEAN));
        //     preferences.add(new AlarmPreference(AlarmPreference.Key.ALARM_VIBRATE, "震动",null, null, alarm.getVibrate(), AlarmPreference.Type.BOOLEAN));
        //	preferences.add(new AlarmPreference(AlarmPreference.Key.ALARM_NAME, "闹钟标签",alarm.getAlarmName(), null, alarm.getAlarmName(), AlarmPreference.Type.STRING));
        preferences.add(new AlarmPreference(AlarmPreference.Key.ALARM_REPEAT, "重复", alarm.getRepeatDaysString(), repeatDays, alarm.getDays(), AlarmPreference.Type.MULTIPLE_LIST));
        preferences.add(new AlarmPreference(AlarmPreference.Key.ALARM_MAC, "设备", alarm.getAlarmMAC(), null, new DevicesListSqlHelper().getSingleDevice(alarm.getAlarmMAC()).getName(), AlarmPreference.Type.ACTIVITY));

        //	preferences.add(new AlarmPreference(AlarmPreference.Key.ALARM_TYPE,"类型", alarm.getAlarmType().toString(), alarmDifficulties, alarm.getAlarmType(), AlarmPreference.Type.LIST));


        //获得系统的铃声
        //		Uri alarmToneUri = Uri.parse(alarm.getAlarmTonePath());
        //	Ringtone alarmTone = RingtoneManager.getRingtone(getContext(), alarmToneUri);
        //
        //	if(alarmTone !=null && !alarm.getAlarmTonePath().equalsIgnoreCase("")){
        //	preferences.add(new AlarmPreference(AlarmPreference.Key.ALARM_TONE, "铃声", alarmTone.getTitle(getContext()),alarmTones, alarm.getAlarmTonePath(), AlarmPreference.Type.LIST));
        //	}else{

        //没有的话就静音
        //	preferences.add(new AlarmPreference(AlarmPreference.Key.ALARM_TONE, "铃声", getAlarmTones()[0],alarmTones, null, AlarmPreference.Type.LIST));
        //	}


    }


    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public String[] getRepeatDays() {
        return repeatDays;
    }

    public String[] getAlarmDifficulties() {
        return alarmDifficulties;
    }

    public String[] getAlarmTones() {
        return alarmTones;
    }

    public String[] getAlarmTonePaths() {
        return alarmTonePaths;
    }

}
